package com.qing.study.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.cj.util.StringUtils;
import com.qing.study.comm.Result;
import com.qing.study.mapper.WitticismSentenceMapper;
import com.qing.study.pojo.WitticismSentence;
import com.qing.study.service.WitticismSentenceService;
import com.qing.study.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
public class WitticismSentenceServiceImpl extends ServiceImpl<WitticismSentenceMapper, WitticismSentence> implements WitticismSentenceService {

    private static final Logger logger = LoggerFactory.getLogger(WitticismSentenceServiceImpl.class);
    @Override
    // @Scheduled(cron = "0 0/2 * * * ?")
    public Result getLinkDataAndAdd() {
        //获取名言警句url
        String url = "https://api.xygeng.cn/one";
        String s = HttpUtil.sendGet(url, "");
        logger.info("获取到的名言警句：{}{}",s,"end......");
        if(!StringUtils.isNullOrEmpty(s)){
            JSONObject jsonObject = JSONObject.parseObject(s);
            WitticismSentence data = JSONObject.parseObject(jsonObject.get("data").toString(), WitticismSentence.class);
            WitticismSentence one = super.getOne(new LambdaQueryWrapper<WitticismSentence>().eq(WitticismSentence::getId, data.getId()));
            if(one!=null){
                super.updateById(data);
                logger.info("获取到重复的名言警句，已经更新数据！");
                return Result.error(200,"获取到重复的名言警句，已经更新数据！");
            }
            super.save(data);
            return Result.ok(data);
        }else {
            return Result.error(500,"获取名言警句异常！");
        }
    }
}
